# encoding=utf-8
from seg_common.logging import ConsoleService
from seg_system.LongTaskStatusUpload import MultiStatusUploadImpl

class ExceptionCallBack:
    logger = ConsoleService.get_logger()
    """
        预测线程池异常处理需要的参数
        如果是多个进程的话，用id区分
    """
    predict_pool_exception_info = {}

    IStateUpload = MultiStatusUploadImpl()

    @staticmethod
    def threading_pool_callback(worker):
        ExceptionCallBack.logger.info('called thread pool executor callback function!!!')
        worker_exception = worker.exception()
        if worker_exception:
            msg = 'Worker error occur: {}'.format(worker_exception)
            ExceptionCallBack.logger.exception(msg)

            redis_id = ExceptionCallBack.predict_pool_exception_info[id(worker)]
            ExceptionCallBack.IStateUpload.update_total(redis_id, has_finished=True)
            ExceptionCallBack.IStateUpload.update_state(redis_id, state=msg)


